package com.crm.user.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.Gson;
import com.crm.base.annotation.Authority;
import com.crm.base.controller.BaseController;
import com.crm.customers.model.ContactsDynamicInfo;
import com.crm.customers.model.CustomerInfo;
import com.crm.customers.service.ContactsDynamicInfoService;
import com.crm.customers.service.CustomerService;
import com.crm.im.core.constant.IMConstant;
import com.crm.im.core.model.Message;
import com.crm.im.core.session.DefaultSessionManager;
import com.crm.im.core.session.IMSession;
import com.crm.sys.model.Department;
import com.crm.sys.model.Role;
import com.crm.sys.model.SysMessage;
import com.crm.sys.model.SysTerm;
import com.crm.sys.model.UserHistoryMessage;
import com.crm.sys.push.SystemMessagePusher;
import com.crm.sys.service.FilesInfoService;
import com.crm.sys.service.SysService;
import com.crm.user.model.ImFriendUserData;
import com.crm.user.model.ImUserData;
import com.crm.user.model.UserAccount;
import com.crm.user.model.UserInfo;
import com.crm.user.service.UserService;
import com.crm.util.Constants;
import com.crm.util.Pager;
import com.crm.util.StringUtil;
@Controller
public class MobileUserController extends BaseController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private SysService sysService;
	@Autowired
	private FilesInfoService filesInfoService;
	@Autowired
	private DefaultSessionManager sessionManager;
	@Autowired
	private SystemMessagePusher systemMessagePusher;
	@Autowired
	private ContactsDynamicInfoService contactsDynamicInfoService;
	@Autowired
	private CustomerService customerService;
	
 
 
	         
	
	/**
	 *  后台首页
	 * @param response
	 * @param request
	 * @param redirectAttributes
	 * @return
	 * @throws Exception
	 */
	@Authority(validdateURL=true)
	@RequestMapping(value = "/mobileindex", method = RequestMethod.GET)
	public String manageIndex(HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		List<SysTerm>  menu = sysService.getRoleMenu(getLoginUser().getInfo().getRid());
		request.setAttribute("robot", Constants.ROBOTNAME);
		request.setAttribute("menuobj", menu);
		return "mobile/index"; 
	}
	
	  
	
	
	/**
	 * @return
	 */
	@RequestMapping(value = "/mlogin", method = RequestMethod.POST)
	public String userLogin(HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes attr) throws Exception{
		getSession().setAttribute("action", 1);
		/*String uname =  request.getParameter("uname"); //账号
		String upwd = request.getParameter("upwd"); //密码
*/		String uname =  request.getParameter("account"); //账号
		String upwd = request.getParameter("password"); //密码
		String codenum = request.getParameter("codenum"); //密码
		
		String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if(codenum != null && kaptchaExpected.equals(codenum.toLowerCase())){
			UserAccount  user = new UserAccount();
			user.setAccount(uname);
			user.setPassword(upwd);
			UserAccount  result = userService.validateLoginUser(user);
			if(result!=null&&result.getInfo()!=null){
				setLoginUser(result,response);
				getSession().setAttribute("errormsg", null);
			}else{
				getSession().setAttribute("errormsg", "账号或密码错误!");
				attr.addFlashAttribute("action",1);
				return "redirect:/";
			}
		}else{
			getSession().setAttribute("errormsg", "验证码错误!");
			attr.addFlashAttribute("action",1);
			return "redirect:/";
		}
		request.getSession().setAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY,")(*&^$#@@!#$");
		
		return  "redirect:/mobileindex"; 
	}
	
	 
}
